package org.dromara.web.plugins.alipay;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class AlipayService implements InitializingBean {

    @Value("${alipay.appid}")
    private String appId;
    @Value("${alipay.privateKey}")
    private String privateKey;
    @Value("${alipay.publicKey}")
    private String publicKey;
    @Value("${alipay.notifyUrl}")
    private String notifyUrl;


    AlipayClient alipayClient;
//

    @Override
    public void afterPropertiesSet() throws Exception {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId(appId);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(publicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");

        alipayClient = new DefaultAlipayClient(alipayConfig);
    }


    public boolean verifyNotifyParam(Map<String, String> params) {
        try {
            return AlipaySignature.rsaCheckV1 (params, publicKey, "utf-8","RSA2");
        } catch (AlipayApiException e) {
            return false;
        }
    }




    public Object alipayPcPrePay(String paymentOrderId, BigDecimal amount) {
        // 构造请求参数以调用接口
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();

        // 设置商户订单号
        model.setOutTradeNo(paymentOrderId);

        // 设置订单总金额
        model.setTotalAmount(amount.toString());
//        model.setSellerId(appId);

        // 设置订单标题
        model.setSubject("会员套餐购买");
        model.setBody("会员套餐");
        // 设置产品码
        model.setProductCode("QR_CODE_OFFLINE");




        // 设置订单包含的商品列表信息
        List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
        GoodsDetail goodsDetail0 = new GoodsDetail();
        goodsDetail0.setGoodsId(paymentOrderId);
        goodsDetail0.setGoodsName("会员套餐");
        goodsDetail0.setQuantity(1L);
        goodsDetail0.setPrice(amount.toString());
        goodsDetail.add(goodsDetail0);
        model.setGoodsDetail(goodsDetail);


        request.setBizModel(model);
        request.setNotifyUrl(notifyUrl);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        try {
            AlipayTradePrecreateResponse response = alipayClient.execute(request);
            // 如果需要返回GET请求，请使用
            // AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "GET");
            String pageRedirectionData = response.getQrCode();


            if (response.isSuccess()) {

            } else {

                // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
                // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
                // System.out.println(diagnosisUrl);
            }
            return pageRedirectionData;
        }catch (Exception e) {
            throw new RuntimeException("支付失败");
        }
    }


}
